# Maintainer: gym603 <gui_yuan_miao@163.com>

_realname=nsync
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.26.0
pkgrel=1
pkgdesc="C library that exports various synchronization primitives (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url="https://github.com/google/nsync"
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/google/nsync/archive/${pkgver}.tar.gz
        0001-nsync-mingw-w64.patch
        0002-nsync-clang-fixes.patch)
sha256sums=('80fc1e605bb3cf5f272811ece39c4fb6761ffcb9b30563301845cc9ff381eb8b'
            'fd06418e181f91d395693c60671caa291bfc7a034c9f7bd21551bdc76a9afd1b'
            '3c33925b71e13845a1fd6fdc5000cdcb94fedfc54f835ab3caedcf725fe5dffa')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${_realname}-${pkgver}"
  apply_patch_with_msg \
    0001-nsync-mingw-w64.patch \
    0002-nsync-clang-fixes.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DNSYNC_ENABLE_TESTS=ON \
    -DBUILD_SHARED_LIBS=ON \
    ../${_realname}-${pkgver}

  cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" cmake --install .

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
